package com.cyan.app.workflow.activity.SFC;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.cyan.app.barcode.CaptureActivity;
import com.cyan.app.workflow.R;
import com.cyan.app.workflow.base.activity.BaseFragmentActivity;
import com.cyan.app.workflow.bean.BaseBean;
import com.cyan.app.workflow.bean.MannoBean;
import com.cyan.app.workflow.bean.MannoLabBean;
import com.cyan.app.workflow.common.HttpConfig;
import com.cyan.app.workflow.customview.ShowTipDialog;
import com.cyan.app.workflow.util.AppUtils;
import com.cyan.app.workflow.util.network.CommHttpListener;
import com.cyan.app.workflow.util.network.VolleyUtil;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by DXW on 2015/11/27.
 */
public class GdlzLzActivity extends BaseFragmentActivity {
    public static final String TAG = "GdlzLzActivity";
    private TextView mTextViewTitle;
    private Button mButtonSubmit;
    private EditText mEditTextKw;
    private EditText mEditTextYg;
    private MannoLabBean mMannoLabBean;
    private Button mButtonKw;
    private Button mButtonYg;
    private static final String YGBH = "gdlz_lz_ygbh";
    private ShowTipDialog mShowTipDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sfc_gdlz_lz);

        initView();
        mEditTextKw = (EditText)findViewById(R.id.sfc_gdlz_lz_kw);
        mEditTextYg = (EditText)findViewById(R.id.sfc_gdlz_lz_ygk);
        mButtonSubmit=(Button)findViewById(R.id.sfc_gdlz_lz_submit);
        mButtonSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /*if(ClickUtil.isFastClick(30)){
                    //30秒内不能重复操作
                    AppUtils.showTip(GdlzLzActivity.this, "正在执行中，请稍等。");
                    return;
                }*/
                if (mEditTextKw.getText().toString().isEmpty()) {
                    AppUtils.showTip(GdlzLzActivity.this, "库位编号不能为空");
                }else if (mEditTextYg.getText().toString().isEmpty()) {
                    AppUtils.showTip(GdlzLzActivity.this, "员工编号不能为空");
                }else if(mEditTextYg.getText().length()>8){
                    AppUtils.showTip(GdlzLzActivity.this, "员工编号太长");
                } else if (mMannoLabBean==null||mMannoLabBean.getMannos().size()==0) {
                    AppUtils.showTip(GdlzLzActivity.this, "没有可流转的工单");
                }else {
                    mButtonSubmit.setClickable(false);
                    execLZ();
                }
            }
        });
        mButtonKw =(Button)findViewById(R.id.sfc_gdlz_lz_kw_sm);
        mButtonKw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(GdlzLzActivity.this, CaptureActivity.class), 0x0002);
            }
        });
        mButtonYg =(Button)findViewById(R.id.sfc_gdlz_lz_ygk_sm);
        mButtonYg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(GdlzLzActivity.this, CaptureActivity.class), 0x0003);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 0x0002 && resultCode == RESULT_OK) {
            String result = "";
            if(data != null) {
                result = data.getStringExtra("result");
            }
            if (!result.contains("10010")) {
                AppUtils.showTip(GdlzLzActivity.this, "库位不存在");
            }else{
                mEditTextKw.setText(String.valueOf(result.substring(5)));
            }
        }
        if(requestCode == 0x0003 && resultCode == RESULT_OK) {
            String result = "";
            if(data != null) {
                result = data.getStringExtra("result");
            }
            if (!result.contains("40010")) {
                AppUtils.showTip(GdlzLzActivity.this, "员工不存在");
            }else {
                mEditTextYg.setText(String.valueOf(result.substring(5)));
                //PreferenceManager.getDefaultSharedPreferences(this).edit().putString(YGBH,result.substring(5)).commit();
            }
        }
    }

    private void initView(){
        titleBar();
        //库位禁止键盘输入
        mEditTextKw = (EditText)findViewById(R.id.sfc_gdlz_lz_kw);
        mEditTextKw.setInputType(InputType.TYPE_NULL);
        //员工禁止键盘输入
        mEditTextYg = (EditText)findViewById(R.id.sfc_gdlz_lz_ygk);
        mEditTextYg.setInputType(InputType.TYPE_NULL);
        /*String ygbh = PreferenceManager.getDefaultSharedPreferences(this).getString(YGBH,null);
        if(ygbh!=null){
            mEditTextYg.setText(String.valueOf(ygbh));
        }*/
        mMannoLabBean = MannoLabBean.get(this);
    }
    private void titleBar(){
        mTextViewTitle =(TextView)findViewById(R.id.top_title_info);
        mTextViewTitle.setText("工单流转提交");
        Button back = (Button) findViewById(R.id.top_title_back);
        back.getBackground().setAlpha(255);// 透明度0~255透明度值 ，值越小越透明
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //返回按钮
                finish();
            }
        });
    }
    //执行流转
    private void execLZ(){
        String result = null;
        mShowTipDialog = new ShowTipDialog();
        mShowTipDialog.showDialog(GdlzLzActivity.this);
        try {
            Map<String, String> params = new HashMap<String, String>();
            JSONArray array = new JSONArray();
            JSONObject jsonObject = new JSONObject();
            for(MannoBean c:mMannoLabBean.getMannos()){
                array.put(c.toJSON());
            }
            jsonObject.put("Keyinuserno",mEditTextYg.getText());
            jsonObject.put("Wareh", mEditTextKw.getText());
            jsonObject.put("Mannoinfo",array.toString());
            params.put("action", "202");
            params.put("params", jsonObject.toString());
            VolleyUtil.sendStringRequestByGet(GdlzLzActivity.this, HttpConfig.HTTP_MAIN_SERVER_URL, TAG, params, null, new CommHttpListener<Object>() {
                @Override
                public void onResponse(String result) {
                    super.onResponse(result);
                    if (result == null) {
                        return;
                    }
                    List<MannoLzResultBean> mannoLzResultBean = new Gson().fromJson(result, new TypeToken<List<MannoLzResultBean>>() {}.getType());
                    String msg = "";
                    for(int i= mannoLzResultBean.size()-1;i>=0;i--){
                        if(mannoLzResultBean.get(i).getCode()==1 && mannoLzResultBean.get(i).getManqty()==mMannoLabBean.getMannos().get(i).getManqty()){
                            mMannoLabBean.deleteCrime(mMannoLabBean.getMannos().get(i));
                        }else if(mannoLzResultBean.get(i).getCode()==1 && mannoLzResultBean.get(i).getManqty()<mMannoLabBean.getMannos().get(i).getManqty()){
                            mMannoLabBean.getMannos().get(i).setNextProssname("待入库");
                            mMannoLabBean.getMannos().get(i).setNextProsscode("A00");
                            int num = mMannoLabBean.getMannos().get(i).getManqty()-mannoLzResultBean.get(i).getManqty();
                            mMannoLabBean.getMannos().get(i).setManqty(num);
                        }
                        else {
                            msg = msg + mMannoLabBean.getMannos().get(i).getManno() + mannoLzResultBean.get(i).getMsg() + ",";
                        }
                    }
                    mShowTipDialog.closeDialog();
                    if(msg == "") {
                        AppUtils.showTip(GdlzLzActivity.this, "流转成功");
                    }else {
                        AppUtils.showTip(GdlzLzActivity.this, msg );
                    }
                    finish();
                }

                @Override
                public void onCodeError(int code, String msg) {
                    mShowTipDialog.closeDialog();
                    AppUtils.showTip(GdlzLzActivity.this, msg);
                }
                @Override
                public void onVolleyError(VolleyError volleyError) {
                    mShowTipDialog.closeDialog();
                    AppUtils.showTip(GdlzLzActivity.this, R.string.network_error);
                }
                @Override
                public void onException(Exception e) {
                    mShowTipDialog.closeDialog();
                    AppUtils.showTip(GdlzLzActivity.this, R.string.network_error);
                }
            });
        } catch (Exception e) {
            mShowTipDialog.closeDialog();
            e.printStackTrace();
            AppUtils.showTip(GdlzLzActivity.this, "执行失败，请联系IT人员");
        }
    }
    class MannoLzResultBean extends BaseBean
    {
        private String Manno;
        private int Manqty;
        public int getManqty() {
            return Manqty;
        }

        public void setManqty(int manqty) {
            Manqty = manqty;
        }

        public String getManno() {
            return Manno;
        }

        public void setManno(String manno) {
            Manno = manno;
        }
    }
}
